import { Fields, TableAction } from "@/types/public";
import { Button, Popconfirm, Space } from "antd";
import React from "react";
import { EditOutlined, DeleteOutlined } from "@ant-design/icons";

export interface Project {
  /** 项目唯一ID，自增主键 */
  projectId: number;

  /** 项目名称（必填，禁止重复） */
  projectName: string;

  /** 项目排序值（默认0，支持拖动调整顺序） */
  sortOrder: number;

  /** 纬度（十进制，保留6位小数） */
  latitude: number;

  /** 经度（十进制，保留6位小数） */
  longitude: number;

  /** 所属国家（级联选择） */
  provinceCountry: string;

  /** 所属省市（级联选择） */
  provinceCity: string;

  /** 项目详细描述（可选） */
  description?: string | null;

  /** 备注信息（可选） */
  remark?: string | null;

  /** 数据推送目标URL（可选） */
  pushUrl?: string | null;

  /** 数据推送认证KEY（可选） */
  pushKey?: string | null;

  /** 记录创建时间 */
  createdAt: string | Date;

  /** 记录最后更新时间 */
  updatedAt: string | Date;
}

export const createProjectField = (
  tableAction: TableAction<Project> = {}
): Fields[] => {
  return [
    {
      valueType: "number",
      inputType: "input",
      key: "projectId",
      title: "项目ID",
      disabled: true,
      dataIndex: "projectId",
      width: 100,
      isEdit: false,
      visible: true,
    },

    {
      valueType: "string",
      inputType: "input",
      title: "项目名称",
      dataIndex: "projectName",
      key: "projectName",
      placeholder: "请输入唯一项目名称",
      rules: [
        {
          required: true,
          message: "项目名称不能为空",
        },
        {
          validator: (_: any, value: any) =>
            !value || value.length <= 128
              ? Promise.resolve()
              : Promise.reject(new Error("项目名称长度不能超过128字符")),
        },
      ],
    },
    {
      valueType: "number",
      inputType: "input-number",
      title: "排序值",
      dataIndex: "sortOrder",
      key: "sortOrder",
      placeholder: ":0",
      initialValue: 0,
      rules: [
        {
          required: true,
          message: "排序值不能为空",
        },
        {
          type: "number",
          min: 0,
          max: 999,
          message: "排序值必须在0~999之间",
        },
      ],
    },
    {
      valueType: "number",
      inputType: "input",
      title: "纬度",
      dataIndex: "latitude",
      key: "latitude",
      placeholder: "例如：30.274084",
      visible: false,
      rules: [
        {
          required: true,
          message: "纬度不能为空",
        },
        {
          pattern: /^-?\d{1,10}(\.\d{1,6})?$/,
          message: "纬度格式应为整数部分最多10位，小数部分最多6位",
        },
      ],
    },
    {
      valueType: "number",
      inputType: "input",
      title: "经度",
      dataIndex: "longitude",
      key: "longitude",
      placeholder: "例如：120.155070",
      rules: [
        {
          required: true,
          message: "经度不能为空",
        },
        {
          pattern: /^-?\d{1,10}(\.\d{1,6})?$/,
          message: "经度格式应为整数部分最多10位，小数部分最多6位",
        },
      ],
    },
    {
      valueType: "string",
      inputType: "cascader",
      title: "所属国家",
      dataIndex: "provinceCountry",
      key: "provinceCountry",
      placeholder: "请选择国家",
      options: [], // 实际使用时应填充国家选项数据
      rules: [
        {
          required: true,
          message: "请选择国家",
        },
      ],
    },
    {
      valueType: "string",
      inputType: "cascader",
      title: "所属省市",
      dataIndex: "provinceCity",
      key: "provinceCity",
      placeholder: "请选择省市",
      options: [], // 实际使用时应填充省市选项数据
      rules: [
        {
          required: true,
          message: "请选择省市",
        },
      ],
    },
    {
      valueType: "string",
      inputType: "textarea",
      title: "项目描述",
      dataIndex: "description",
      key: "description",
      placeholder: "请输入项目详细描述",
      autoSize: { minRows: 3, maxRows: 6 },
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "textarea",
      title: "备注",
      dataIndex: "remark",
      key: "remark",
      placeholder: "请输入备注信息",
      autoSize: { minRows: 2, maxRows: 4 },
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "推送URL",
      dataIndex: "pushUrl",
      key: "pushUrl",
      placeholder: "https://example.com/api",
      rules: [
        {
          required: false,
          type: "url",
          message: "请输入有效的URL地址",
        },
        {
          validator: (_: any, value: any) =>
            !value || value.length <= 256
              ? Promise.resolve()
              : Promise.reject(new Error("URL长度不能超过256字符")),
        },
      ],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "推送KEY",
      dataIndex: "pushKey",
      key: "pushKey",
      placeholder: "输入认证密钥",
      rules: [
        {
          required: false,
          validator: (_: any, value: any) =>
            !value || value.length <= 128
              ? Promise.resolve()
              : Promise.reject(new Error("KEY长度不能超过128字符")),
        },
      ],
    },
    {
      valueType: "string",
      inputType: "date-picker",
      title: "创建时间",
      dataIndex: "createdAt",
      key: "createdAt",
      placeholder: "系统自动生成",
      disabled: true,
      visible: true,
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "date-picker",
      title: "更新时间",
      dataIndex: "updatedAt",
      key: "updatedAt",
      placeholder: "系统自动更新",
      disabled: true,
      visible: true,
      rules: [{ required: false }],
    },

    {
      title: "操作",
      key: "action",
      fixed: "right",
      width: 300,
      render: (_: any, record: Project) => (
        <Space size="middle">
          <Button
            type="link"
            size="small"
            icon={<EditOutlined />}
            onClick={() => tableAction.edit?.(record)}
          >
            编辑
          </Button>
          <Popconfirm
            title="确定删除该项目？"
            onConfirm={() => tableAction.delete?.([record.projectId])}
          >
            <Button type="link" danger icon={<DeleteOutlined />}>
              删除
            </Button>
          </Popconfirm>
          <Button
            type="primary"
            size="small"
            onClick={() => tableAction.migrate?.()}
          >
            项目迁移
          </Button>
        </Space>
      ),
    },
  ];
};
